package com.qkst.website.web.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.web.bean.UserBean;
import com.qkst.website.web.mapper.UserDetailMapper;
import com.qkst.website.web.mapper.UserMapper;
import com.qkst.website.web.service.UserService;

/**
 * @author lihao
 * @description 用户service
 * @date 2017-05-08
 * @email ahlxlihao@163.com
 *
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserDetailMapper userDetailMapper;
	/**
	 * 根据订单条件查询用户
	 * @param user
	 * @return List<UserBean>
	 */
	public List<UserBean> queryUserList(UserBean user){
		//分页计算
		return userMapper.selectUserList(user);
	}
	
	/**
	 * 查询用户总记录数
	 * @param order
	 * @return 
	 */
	@Override
	public int queryCount(UserBean user) {
		return userMapper.selectCount(user);
	}

	
	/**
	 * 注册用
	 * @param order
	 * @return boolean
	 */
	@Override
	public void save(UserBean user,ModelAndView mv) {
		user.setCreateTime(new Date());
		user.setLastUpdate(new Date());
		user.setLastLoinTime(new Date());
		int num=userMapper.insert(user);//插入用户基本信息
		mv.addObject("loginUser",user);
		if(num>0){//插入用户基本信息成功
			//插入用户明细
			user.getUserDetail().setUserId(user.getUserId());
			userDetailMapper.insert(user.getUserDetail());
			mv.addObject("isSuccess",true);
			mv.addObject("code", "000");
		}else{//插入失败
			mv.addObject("isSuccess",false);
			mv.addObject("code", "500");
		}
	}
		
	/**
	 * 拉黑/禁用用户
	 * @param order
	 * @return 
	 */
	@Override
	public void forbidUser(UserBean user, Map<String, Object> result) {
		if(user.getUserId()!=null){
			Short status=-1;
			user.setStatus(status);//禁用
			user.setLastUpdate(new Date());
			int num=userMapper.updateByPrimaryKeySelective(user);
			result.put("user",user);
			if(num>0){//拉黑/禁用成功
				result.put("isSuccess", true);
				result.put("code", "000");
			}else{//拉黑/禁用失败
				result.put("isSuccess", false);
				result.put("code", "500");
			}
		}else{
			result.put("isSuccess", false);
			result.put("code", "404");
		}
		
	}

	@Override
	public UserBean findUserByCondition(UserBean user) {
		return userMapper.findUserByCondition(user);
	}

	@Override
	public void updateUser(UserBean loginUser) {
		int num=userMapper.updateByPrimaryKeySelective(loginUser);//更新用户基本信息
		if(num>0){//更新用户基本信息成功
			//更新用户明细
			loginUser.getUserDetail().setUserId(loginUser.getUserId());
			userDetailMapper.updateByPrimaryKeySelective(loginUser.getUserDetail());
		}else{//插入失败
		}
		
	}
	
}
